package com.atguigu.cloud.controller;/**
 * @Auter zzh
 * @Date 2024/11/13
 */

import com.atguigu.cloud.entities.Product;
import com.atguigu.cloud.sevice.Impl.ProductServiceImpl;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.controller
 * @className: productcontrollor
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 18:30
 * @version: 1.0
 */
@Tag(name = "商品表", description = "商品表增删改")
@RestController
public class Productcontrollor {
    @Resource
    ProductServiceImpl productService;


    //增加商品
    @PostMapping(value = "/product/add")
    @Operation(summary = "添加商品", description = "添加商品")
    public ResultData<String> addproduct(@RequestBody Product product){
        System.out.println(product.toString());
        var i = productService.add(product);
        return ResultData.success("成功插入记录，返回值："+i);
    }

    //删除商品
    @DeleteMapping(value = "/product/del/{id}")
    @Operation(summary = "删除商品", description = "删除商品")
    public ResultData<Integer> deleteproduct(@PathVariable("id") Integer id) {
        Integer i=productService.delete(id);
        return ResultData.success(i);
    }

    //修改商品
    @PutMapping(value = "/product/update")
    @Operation(summary = "修改商品", description = "修改商品")
    public ResultData<String> updateproduct(@RequestBody Product product){
        if (product.getProname()==null){
            product.setProname("待命名商品");
        }
        int i = productService.update(product);
        return ResultData.success("成功修改记录，返回值："+i);
    }

    //查询商品
    @GetMapping(value = "/product/get/{id}")
    @Operation(summary = "查询商品", description = "查询商品")
    public ResultData<Product> getById(@PathVariable("id") Integer id){
        Product product=productService.getById(id);
        return ResultData.success(product);
    }

    //查询所有商品
    @GetMapping(value = "/product/getall")
    @Operation(summary = "查询所有商品", description = "查询所有商品")
    public ResultData<List<Product>> getAll(){
        return ResultData.success(productService.getAll());
    }

    //按照价格区间搜索商品
    @GetMapping(value = "/product/getbyprice/{min}/{max}")
    @Operation(summary = "按照价格区间搜索商品", description = "可以输入两个数值,且min和max可以为空,且必须为整数," +
            "min默认为0,max默认为100000")
    public ResultData<List<Product>> getByPrice(@PathVariable("min") Integer min, @PathVariable("max") Integer max){
        return ResultData.success(productService.getByPrice(min, max));
    }
    //模糊查询
    @GetMapping(value = "/product/getbylike/{proname}")
    @Operation(summary = "模糊查询", description = "传入模糊查询的文本")
    public ResultData<List<Product>> getByLike(@PathVariable("proname") String proname){
        return ResultData.success(productService.getByLike(proname));
    }
    //按照类别搜索商品
    @GetMapping(value = "/product/getbydistribution/{category}")
    @Operation(summary = "按照类别搜索商品", description = "传入类别,类别必须是正确的类别,不然会返回空")
    public ResultData<List<Product>> getByDistribution(@PathVariable("category") String category){
        return ResultData.success(productService.getByDistribution(category));
    }

    //分页
    //查询所有商品
    @GetMapping(value = "/product/pagination/getall/{currentPage}")
    @Operation(summary = "分页查询所有商品", description = "查询所有商品")
    public ResultData<PaginationResult<Product>> getPaginationAll(@PathVariable("currentPage") Integer currentPage){
        return ResultData.success(productService.getPaginationAll(currentPage));
    }

    //按照价格区间搜索商品
    @GetMapping(value = "/product/pagination/getbyprice/{min}/{max}/{currentPage}")
    @Operation(summary = "分页的按照价格区间搜索商品", description = "可以输入两个数值,且min和max可以为空,且必须为整数," +
            "min默认为0,max默认为100000")
    public ResultData<PaginationResult<Product>> getPaginationByPrice(@PathVariable("min") Integer min,
                                                                      @PathVariable("max") Integer max,
                                                                      @PathVariable("currentPage") Integer currentPage
                                                                      ){
        return ResultData.success(productService.getPaginationByPrice(min, max,currentPage));
    }
    //模糊查询
    @GetMapping(value = "/product/pagination/getbylike/{proname}/{currentPage}")
    @Operation(summary = "分页的模糊查询", description = "传入模糊查询的文本")
    public ResultData<PaginationResult<Product>> getPaginationByLike(@PathVariable("proname") String proname,
                                                                     @PathVariable("currentPage") Integer currentPage
                                                                     ){
        return ResultData.success(productService.getPaginationByLike(proname,currentPage));
    }
    //按照类别搜索商品
    @GetMapping(value = "/product/pagination/getbydistribution/{category}/{currentPage}")
    @Operation(summary = "分页的按照类别搜索商品", description = "传入类别,类别必须是正确的类别,不然会返回空")
    public ResultData<PaginationResult<Product>> getPaginationByDistribution(@PathVariable("category") String category,
                                                                             @PathVariable("currentPage") Integer currentPage
                                                                             ){
        return ResultData.success(productService.getPaginationByDistribution(category,currentPage));
    }


}
